using System;
using System.Collections.Generic;

namespace Randomize.Core.Constructors
{
    public class ComplexTypeConstructor<TComplex>  : IComplexTypeConstructor<TComplex>
    {
        private readonly List<IPropertyConstructor<TComplex>> _propertyConstructors;

        public Func<TComplex> ConstructingFunction { get; set; }

        public ComplexTypeConstructor(List<IPropertyConstructor<TComplex>> propertyConstructors)
        {
            _propertyConstructors = propertyConstructors;
        }

        public TComplex Generate()
        {
            TComplex instance = this.CreateInstance();

            _propertyConstructors.ForEach(propertyConstructor => propertyConstructor.ConstructProperty(instance));

            return instance;
        }

        private TComplex CreateInstance()
        {
            if (this.ConstructingFunction != null)
            {
                return this.ConstructingFunction();
            }

            return Activator.CreateInstance<TComplex>();
        }
    }
}